package com.zhugang.week04;

/**
 * @program algorithms
 * @description: mergeTwoLists
 * @author: chanzhugang
 * @create: 2022/06/24 00:42
 */
public class MergeTwoLists2 {

    /**
     * 剑指offer25 合并两个排序的链表
     *
     * @param l1
     * @param l2
     * @return
     */
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) return l2;
        if (l2 == null) return l1;
        // 递归实现：画图更好理解
        if (l1.val < l2.val) {
            ListNode subHead = mergeTwoLists(l1.next, l2);
            l1.next = subHead;
            return l1;
        } else {
            ListNode subHead = mergeTwoLists(l1, l2.next);
            l2.next = subHead;
            return l2;
        }
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }

        public ListNode() {

        }
    }
}